import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yp/config/index.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/mall_app/my_gas/gas_add/view.dart';

import 'logic.dart';
import 'state.dart';

class MyGasPage extends StatelessWidget {
  final MyGasLogic logic = Get.put(MyGasLogic());
  final MyGasState state = Get.find<MyGasLogic>().state;

  Widget _unBindButton(String gasCode) {
    return GestureDetector(
        onTap: () {
          showDialog(
              context: Get.context,
              barrierDismissible: false,
              builder: (BuildContext context) {
                return AlertDialog(
                    title: Text('提示'),
                    content: Text('确定要解绑燃气账号吗？'),
                    actions: <Widget>[
                      FlatButton(
                        onPressed: () => Navigator.of(context).pop(),
                        child: Text('取消'),
                      ),
                      FlatButton(
                          onPressed: () => logic.unBindNaturalGas(gasCode),
                          textColor: Colors.red,
                          child: Text('确定'))
                    ]);
              });
        },
        child: Container(
            height: 30,
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: 15),
            decoration: BoxDecoration(
                color: Color(0xFFEFEFF4),
                borderRadius: BorderRadius.all(Radius.circular(20))),
            child: Text('解绑')));
  }

  Widget _cell(Map model) {
    TextStyle _labelStyle = TextStyle(color: EsColors.Content);
    return GestureDetector(
        child: Container(
            decoration: BoxDecoration(color: Colors.white),
            padding: EdgeInsets.symmetric(vertical: 12, horizontal: 16),
            margin: EdgeInsets.only(bottom: 2),
            child: Column(children: [
              Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
                Expanded(
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                      Row(children: [
                        Text('姓名: ', style: _labelStyle),
                        Text(model['usr_name']),
                        if (model["gas_association"] != null &&
                            model["gas_association"] != "")
                          Container(
                            color: Config.mainColor,
                            padding: EdgeInsets.symmetric(
                              horizontal: 10,
                              vertical: 0,
                            ),
                            margin: EdgeInsets.only(left: 10),
                            child: Text(
                              model["gas_association"],
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 24.sp,
                              ),
                            ),
                          ),
                      ]),
                      Row(children: [
                        Text('燃气号: ', style: _labelStyle),
                        Text(model['gas_code'])
                      ])
                    ])),
                _unBindButton('${model['gas_code']}')
              ]),
              Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
                Text('地址: ', style: _labelStyle),
                Expanded(child: Text(model['adr']))
              ])
            ])),
        onTap: () {
          if (state.from == 'point_exchange') {
            // 弹框提示
            showDialog(
                context: Get.context,
                barrierDismissible: false,
                builder: (BuildContext context) {
                  return AlertDialog(
                      title: Text('提示'),
                      content: Text('确定要为燃气号' + model['gas_code'] + '充值吗？'),
                      actions: <Widget>[
                        FlatButton(
                          onPressed: () => Navigator.of(context).pop(),
                          child: Text('取消'),
                        ),
                        FlatButton(
                            onPressed: () {
                              print(
                                  'from: ${state.from}, gasCode: ${model['gas_code']}');
                              state.gasCode = '${model['gas_code']}';
                              Get.back();
                              logic.onBack();
                            },
                            textColor: Colors.red,
                            child: Text('确定'))
                      ]);
                });
          }
        });
  }

  Widget _bindButton() {
    return GestureDetector(
        child: Container(
            height: setWidth(88),
            width: ScreenUtil().screenWidth - 32,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(setWidth(44)),
              color: Color(0xffEA5504),
            ),
            margin: EdgeInsets.only(bottom: 30),
            alignment: Alignment.center,
            child: Text('添加账户',
                style: TextStyle(color: Colors.white, fontSize: 16))),
        onTap: () async {
          var result =
              await Get.to(GasAddPage(), arguments: {'from': 'point_exchange'});
          state.gasCode = result['gasCode'];
          state.from = result['from'];
        });
  }

  @override
  Widget build(BuildContext context) {
    print('======================  ${state.dataArray.length}');
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          centerTitle: true,
          elevation: setWidth(1),
          title: Text(
            '我的燃气',
            style: TextStyle(
              color: Color(0xff222222),
              fontSize: 17,
            ),
          ),
          iconTheme: IconThemeData(
            color: Color(0xff222222),
          ),
        ),
        body: Column(children: [
          Expanded(
              child: Container(
                  color: Color(0xffF5F5F5),
                  child: ListView.builder(
                      itemCount: state.dataArray.length,
                      itemBuilder: (context, index) {
                        return _cell(state.dataArray[index]);
                      }))),
          _bindButton()
        ]));
  }
}
